package com.vast.vastlcd.lcdAPI;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * 
 */
public class LCDSetTitleRequest {
	public LCDMessageHeader lcdMessageHeader = new LCDMessageHeader();
	public byte[] szTitle; // = new byte[LCD_SETTITLE_ARRAY_SIZE];	
	
	public static int LCD_SETTITLE_ARRAY_SIZE = 100;
	
	/**
	 * @return
	 */
	public byte[] javaToNative () {
	 	if (this == null) 
	 		return null;
	 	
	 	byte[] buffer = null;
	 	try {
			// write data to a byte array and then ask super to convert to pMedium
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			LCDDataOutputStream writeOut = new LCDDataOutputStream(out);
			
			writeOut.write(lcdMessageHeader.javaToNative());
			
			writeOut.write(szTitle);
			
			// If we don't pad then sometimes it doesn't work - is this only for 
			// the current LCDView
			byte[] padding = new byte[LCD_SETTITLE_ARRAY_SIZE - szTitle.length];
			writeOut.write(padding);
			buffer = out.toByteArray();
			writeOut.close();			
			
		} catch (IOException e) {

		}
		return buffer;
	}
	
	/**
	 * @param buffer
	 * @return
	 */
	static public LCDSetTitleRequest nativeToJava(byte[] buffer){	
	 
		if (buffer == null) 
			return null;
		
		LCDSetTitleRequest str = new LCDSetTitleRequest();
		try {
			ByteArrayInputStream in = new ByteArrayInputStream(buffer);
			LCDDataInputStream readIn = new LCDDataInputStream(in); 			
			 
			// In a Set Title Request the whole body (after the header) is
			// the title request
			
			str.szTitle = new byte[buffer.length];
			readIn.read(str.szTitle);
			//System.out.println("size " + buffer.length + "string " + str.szTitle);	
			readIn.close();
		} catch (IOException ex) {
			return null;
		}
 		
 		return str; 		
	}
	 
	/**
	 * @param msgBodyBuffer
	 */
	static void processMessage(byte[] msgBodyBuffer) {
		LCDSetTitleRequest str = nativeToJava(msgBodyBuffer);		 
		str.updateViewer();		
	}
	
	/**
	 * 
	 */
	void updateViewer() {		
		//while (!PlatformUI.isWorkbenchRunning())
		//{}
//		if (PlatformUI.isWorkbenchRunning()) {
//			 
//		    IWorkbench wb = PlatformUI.getWorkbench();
//			final Display display = wb.getDisplay();
//			
//					
//			display.asyncExec (new Runnable () {
//				public void run () {
//				 	while (display.isDisposed())
//					{}
//				 	
//				 	IWorkbench wb = PlatformUI.getWorkbench();
//				 
//				 	int endStrIndex = szTitle.length - 1;
//				 	for (int i = 0; i < szTitle.length; i++) {
//				 		if (szTitle[i] == 0) {
//				 			endStrIndex = i;
//				 			break;
//				 		}
//				 	}
//				 	
//				 	String szTitleStr = new String(szTitle);
//				 	szTitleStr = szTitleStr.substring(0, endStrIndex);
//				 	
//				 	IWorkbenchWindow workbenchWindow = wb.getActiveWorkbenchWindow(); 
//					if (workbenchWindow == null)
//						return;
//					
//					Shell shell = workbenchWindow.getShell();
//					
//					shell.setText
//					(
//						szTitleStr +  " - VaSTLCD"
//					);				 
//				 }
//			 });
//		 }
	 }
}
